package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.ScoreDto;
import com.zhangtai.modules.entity.score.ScoreEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-14 15:03:55
 */
@Mapper
public interface ScoreDao extends BaseMapper<ScoreEntity> {


    @Select("<script>SELECT\n" +
            "\tts.*,\n" +
            "\ttsp.program_name \n" +
            "FROM\n" +
            "\ttb_score ts\n" +
            "\tLEFT JOIN tb_score_program tsp ON ts.program_id = tsp.id" +
            "\tLEFT JOIN tb_employee_info tei ON ts.oid = tei.oid" +
            "<where> ts.is_delete = 0 " +

            "<if test='orgIds!=null and orgIds.size>0'> and ts.unit_id in " +
            "<foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+
            "<if test='orgIds==null or orgIds.size==0'> and ts.unit_id = null </if>" +

            "<if test='status!=null and status.size>0'> and ts.status in " +
            "<foreach item=\"orgId\" index=\"index\" collection=\"status\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+
            "<if test='status==null or status.size==0'> and ts.status = null </if>" +

            "<if test='orgname!=null and orgname.size>0'> and ts.org_id in " +
            "<foreach item=\"orgId\" index=\"index\" collection=\"orgname\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+

            "<if test='unitname!=null and unitname.size>0'> and ts.unit_id in " +
            "<foreach item=\"orgId\" index=\"index\" collection=\"unitname\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
            "</if>"+

            "<if test='programId!=null'> and ts.program_id = #{programId} </if>" +
            "<if test='name!=null'> and ts.name like '%${name}%'</if>" +
            "<if test='name!=null'> and tei.code like '%${code}%'</if>" +
            "</where>  ORDER BY ts.create_time DESC" +
            "</script>")
    public List<ScoreDto> getScorePage(@Param("orgIds") List<Long> orgIds,
                                       @Param(value = "serialNumber") String serialNumber,
                                       @Param(value = "status") List<Integer> status,
                                       @Param(value = "programId") String programId,
                                       @Param(value = "orgname") List<Long> orgname,
                                       @Param(value = "unitname") List<Long> unitname,
                                       @Param(value = "name") String name,
                                       @Param(value = "code") String code,
                                                        Page<ScoreDto> page);

    @Select("<script>SELECT\n" +
            "\tts.*,\n" +
            "\ttsp.program_name \n" +
            "FROM\n" +
            "\ttb_score ts\n" +
            "\tLEFT JOIN tb_score_program tsp ON ts.program_id = tsp.id" +
            "<where> ts.is_delete = 0 " +
            " and ts.oid = #{oid}" +
            "</where>  ORDER BY ts.create_time DESC" +
            "</script>")
    public List<ScoreDto> getScoreByOidPage(@Param(value = "oid") String oid,
                                       Page<ScoreDto> page);

    @Select("SELECT count(id) FROM tb_score WHERE inset_date = #{insetDate}")
    Long getScoreNum(@Param("insetDate")String insetDate);

}
